<?php

/**
 * Template Name: user_profile
 *
 * 通用的footer
 * date:2011/4/11 
 * @author Ryan 
 * 
 */


get_header();
$templateurl = get_bloginfo('template_url');

$user = $_SESSION['userdata'];

if ($user){
	
	$action = $_POST['action'];
	
	if ($action == 'modify'){
		$password = stripslashes($_POST['password']);
		$display_name = stripslashes($_POST['display_name']);
		$user_sex = $_POST['user_sex'];
		$user_birth_year = $_POST['user_birth_year'];
		$user_birth_month = $_POST['user_birth_month'];
		
		$user_id = $user->ID;
		
		if ($password)
			$userdata = array('user_pass'=>wp_hash_password($password), 
							'display_name'=>$display_name);
		else
			$userdata = array('display_name'=>$display_name);
		
		$usermeta = array('user_sex'=>$user_sex,
					'user_birth_year' =>$user_birth_year,
					'user_birth_month'=>$user_birth_month);
		
		update_user($userdata, $usermeta, $user_id);
		
		$user = get_userdata($user_id);
		
		$_SESSION['userdata'] = $user ;
		
	}
?>

<script type="text/javascript">

var passError = false;
var passError2 = false;

function checkPass1(){
	var pass = $('#pass_input1').val();
	if(pass.length <= 6){
		$('#passstatus').html('密码应大于6个字符！');
		$('#passstatus').css({'color': 'red'});
		passError = false;
	}else{
		$('#passstatus').html('');
		passError = true;
	}
}

function checkPass2(){
	var pass1 = $('#pass_input1').val();
	var pass2 = $('#pass_input2').val();
	if(pass1 != pass2){
		$('#passstatus2').html('两次输入密码不一致！');
		$('#passstatus2').css({'color': 'red'});
		passError2 = false;
	}else{
		$('#passstatus2').html('');
		passError2 = true;
	}
}

function checkSubmit(){
	return $('#pass_input1').val() != '' ? passError && passError2 : true;
}

<?php if($user->login_type == 'sina'){?>
WB2.anyWhere(function(W){
	W.parseCMD("/users/show.json", function(sResult, bStatus){
	    if(bStatus == true) {
		    var user_p = "<ul><li><img src='" + sResult.profile_image_url + "' />"+ sResult.screen_name+"</li>";
		    user_p += "<li><span>省市:</span>" + sResult.location + "</li>";
		    user_p += "<li><span>性别:</span>" + (sResult.gender == 'f' ? '女' : (sResult.gender == 'm' ? '男' : '未知')) + "</li>";
		    user_p += "<li><span>粉丝:</span>" + sResult.followers_count  + "</li>";
		    user_p += "<li><span>关注:</span>" + sResult.friends_count + "</li>";
		    user_p += "<li><span>微博:</span>" + sResult.statuses_count  + "</li></ul>";
		    $("#detail_pro").html(user_p);
	    }
	},{
		screen_name : '<?php echo $user->display_name?>'
	},{
	    method: 'get'
	});
});
<?php }?>

</script>
	<div id="UC_left" class="UC_content">
		<span id="ucent"><samp></samp>用户中心</span>
		<ul>
			<li id="UC_xuanding">个人信息</li>
			<li class="UC_no"><a href="/user_openedgame">玩过的游戏</a></li>
			<li class="UC_no"><a href="/user_collectedgame">收藏的游戏</a></li>
		</ul>
		<img src="<?php echo $templateurl;?>/images/usercenter_tc.gif" id="UCL_bg" alt="游戏搜搜用户中心" title="游戏搜搜用户中心"  />
		<img src="<?php echo $templateurl;?>/images/usercenter_tr.gif" id="UCL_cr" alt="游戏搜搜用户中心" title="游戏搜搜用户中心"  />
	</div>
	<div id="UC_right" class="UC_content">
		<span class="ucright_sider">个人信息</span>
		<div id="userxin" class="ucright_cont">
		<?php if($user->login_type=='sina'){
		?>
			<div id="user_profile_out">
				<p>你好： <?php echo $user->display_name;?>，你正使用新浪账号登录本站，以下将显示你在新浪的微博资料。</p>
				<div id="detail_pro">
					<img src="<?php echo $templateurl;?>/images/loading1.gif" /> 正在加载...
				</div>
			</div>
		<?php
		}else{
		?>
			<form action="/user_profile" method="post" onsubmit="return checkSubmit();">
				<table>
					<tr>
						<td valign="middle">邮&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;箱：</td>
						<td colspan="2"><input type="text" id="email_input" name="email" class="inputstyle02 inputstyle" onblur="checkemail();" disabled="disabled" value="<?php echo $user->user_login;?>"/></td> 
					</tr>
					<tr>
						<td valign="middle">密&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;码：</td>
						<td><input type="password" id="pass_input1" name="password" class="inputstyle02 inputstyle" onblur="checkPass1()" /></td>
						<td><p id="passstatus"></p></td>
					</tr>
					<tr>
						<td valign="middle">确认密码：</td>
						<td><input type="password" id="pass_input2" name="password" class="inputstyle02 inputstyle" onblur="checkPass2()" /></td> 
						<td><p id="passstatus2"></p></td>
					</tr>
					<tr>
						<td valign="middle">昵&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;称：</td>
						<td colspan="2"><input type="text" name="display_name" class="inputstyle02 inputstyle" value="<?php echo $user->display_name;?>" /></td> 
					</tr>
					<tr>
						<td valign="middle">性&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;别：</td>
						<td colspan="2"> <select name="user_sex" class="inputstyle02 inputstyle">
							<option value="">——请选择——</option>
							<option value="男" <?php echo $user->user_sex == '男' ? ' selected="selected" ' : ''; ?> >男</option>
							<option value="女" <?php echo $user->user_sex == '女' ? ' selected="selected" ' : ''; ?>  >女</option> 
							</select>
						</td>
					</tr>
					 <tr>
						<td valign="middle">生&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;日：</td>
						<td colspan="2">
							<input type="text" name="user_birth_year" class="inputstyle01 inputstyle" value="<?php echo $user->user_birth_year;?>" />&nbsp;&nbsp;年
							<input type="text" name="user_birth_month" class="inputstyle01 inputstyle" value="<?php echo $user->user_birth_month;?>" />&nbsp;&nbsp;月
							<input type="hidden" name="action" value="modify">
						</td> 
					</tr>
					<tr>
						<td valign="middle"> </td>
						<td colspan="2"><input type="submit" value="修改信息" class="inputstyle03" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
						</td> 
					</tr>
				</table>
			</form> 
			<?php }?>
		</div>
	</div>
<?php }else {
	header("Location: ".get_bloginfo('home').'/login');
}

function update_user($userdata, $user_meta, $user_id){
	global $wpdb;
	$d = $wpdb->update('wp_users', $userdata, array('ID'=>$user_id));
	
	update_user_meta($user_id, 'user_sex', $user_meta['user_sex']);
	update_user_meta($user_id, 'user_birth_year', $user_meta['user_birth_year']);
	update_user_meta($user_id, 'user_birth_month', $user_meta['user_birth_month']);
	
	return $useid;
}
?>
	
<?php get_footer();?>